/**
 * Heatmap.js
 * Version: 1.0
 * Author: wyh
 *
 */
var Heatmap = function (HeatmapData) {

    //展示图层url,geojson格式，点图层，热力图分布与点密度有关
    var _heatmapdata;
    var _blur = 50;  //默认模糊尺寸，建议范围1-50 30
    var _radius = 20; //默认热点半径，建议范围1-50 20
    var _heatMapLayer;

    this.setHeatmapData = function (heatmapdata) {

        _heatmapdata = heatmapdata;
    };
    this.getHeatmapData = function () {
        return _heatmapdata;
    };

    this.setBlur = function (blur) {

        _blur = blur;
    };
    this.getBlur = function () {
        return _blur;
    };

    this.setRadius = function (radius) {

        _radius = radius;
    };
    this.getRadius = function () {
        return _radius;
    };

    this.setHeatmapData(HeatmapData);

    this.showHeatmap = function () {

        var vectorSource = new ol.source.Vector();

        for (var i = 0; i < _heatmapdata.length; i++) {

            try {
                var curpt = _heatmapdata[i];
                var x = curpt.x;
                var y = curpt.y;
                var weight = curpt.weight/10;
                if(isNaN(weight))
                {
                    weight=1;
                }


                for (var j = 0; j < weight; j++) {
                    var newFeature = new ol.Feature();
                    newFeature.setGeometry(new ol.geom.Point(ol.proj.transform([x, y], 'EPSG:4326', 'EPSG:3857')));
                    newFeature.set('weight', weight);
                    vectorSource.addFeature(newFeature);
                }

            } catch (e) {
                console.log(e.name + ": " + e.message + "----" + i);
            }
        }

        //定义热力图
        _heatMapLayer = new ol.layer.Heatmap({
            title: '热力图',
            visible: true,
            /*source: new ol.source.Vector({
                //json格式点图层，点密度决定热力图分布
                url: _url,
                format: new ol.format.GeoJSON()
            }),*/
            blur: _blur, //模糊程度
            radius: _radius  //半径
        });

        _heatMapLayer.setSource(vectorSource);
        YWLayer.getLayers().push(_heatMapLayer); //后续封装完善YWLayer也需作为参数传入
    };

    this.removeHeatmap = function () {
        YWLayer.getLayers().remove(_heatMapLayer);
    };

}


